package yxy.game.pm2.active.command.ghost;

import yxy.apple.logger.Logger;
import yxy.apple.util.RandomUtility;
import yxy.cherry.data.bean.actghost;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_神魔卡;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.ghost.GhostRandom;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

/**
 * 神魔卡_抽将 抽将
 * @author Dell
 *
 */
@MessageType(proto = Command.ActGhostGachaHero, classtype = ActiveMessage.ActGhostGachaHeroRequest.class)
public class CMD_Active_神魔卡_抽将_Gacha extends Command<ActiveMessage.ActGhostGachaHeroRequest> {

	private static final Logger logger = Logger.create(CMD_Active_神魔卡_抽将_Gacha.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActGhostGachaHeroRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int type = request.getType();
		ActiveMessage.ActGhostGachaHeroResponse.Builder response = ActiveMessage.ActGhostGachaHeroResponse.newBuilder();
		
		ActiveModule_神魔卡 am = ActiveModule_神魔卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getGhostmap().size() == 0) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(type != ActiveModule_神魔卡.gachatype1 && type != ActiveModule_神魔卡.gachatype10) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		actghost ag = ActDataCenter.getData(101, actghost.class, playercode);
			
		Currency costcur = Currency.create(ag.getCosttype(), ag.getCostid(), ag.getCostnum() * type);
	
		boolean cangacha = new CurrencyHandle().costCheck(playercode, costcur);
		if(!cangacha) {
			send(messageSource, response, new Result(Result.CostFail, costcur.getType().getNumber(), costcur.getDataId()));
			return;
		}
		
		int idealcondition = ag.getIdealcondition();
		
		//心仪武将
		HashMap<Integer, Currency> curmap = new HashMap<Integer, Currency>();	//心仪武将	//key:标记数 vaule:奖励
		int[] indicias = ag.getIndicia();
		int[] idealtypes = ag.getIdealrewardtype();
		int[] idealids = ag.getIdealid();
		int[] idealnums = ag.getIdealrewardnum();
		for(int i = 0; i < indicias.length; i++) {
			ActiveMessage.HeartHero.Builder build = ActiveMessage.HeartHero.newBuilder();
			
			int indicia = indicias[i];
			int idealtype = idealtypes[i];
			int idealid = idealids[i];
			int idealnum = idealnums[i];
			
			Currency cur = Currency.create(idealtype, idealid, idealnum);
			curmap.put(indicia, cur);
		}
		
		//奖池处理
		int[] curtypes = ag.getRewardtype();
		int[] curids = ag.getRewardid();
		int[] curnum = ag.getRewardnum();
		int[] percent = ag.getPercent();
		
		ArrayList<GhostRandom> grs = new ArrayList<GhostRandom>();
		for(int i = 0; i < curtypes.length; i++) {
			Currency cur = Currency.create(curtypes[i], curids[i], curnum[i]);
			GhostRandom gr = new GhostRandom(cur, percent[i]);
			grs.add(gr);
		}
		
		ArrayList<Currency> curs = new ArrayList<Currency>();
		
		//开始抽奖
		int newcknum = am.getCknum();
		Currency signcur = curmap.get(am.getSelectid());
		for(int i = 0; i < type; i++) {
			newcknum++;
			if(newcknum % idealcondition == 0) {
				curs.add(signcur);
				continue;
			}
			
			GhostRandom gr = RandomUtility.randomWeight(grs);	//权重随机
			curs.add(gr.getCur());
		}
		
		try {
			new CurrencyHandle().cost(playercode, CCReason.活动_神魔卡, costcur);
		} catch (Exception e) {
			send(messageSource, response, Result.CostFail);
			return;
		}
		
		for(Currency cur : curs) {
			if(cur.getType().getNumber() == signcur.getType().getNumber() && cur.getDataId() == signcur.getDataId() && cur.getNumber() == signcur.getNumber()) {
				TaskHandle.handle(playercode, TaskType.统计型_神魔卡_抽取限定武将_次数, 1);
			}
		}
		
		am.setSelectid(am.getSelectid());
		am.setCknum(newcknum);
		am.set();
		
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
		response.addAllDisplay(items);
		
		new CurrencyHandle().addCurrency(playercode, CCReason.活动_神魔卡, curs);
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActGhostGachaHeroResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
